<?php
require_once 'BaseController.php';

/**
 * 【PC版】店舗一覧 コントローラ
 */
class Renew_ShoplistController extends Renew_BaseController
{

    /**
     * メインアクション
     */
    public function indexAction()
    {
        $params = $this->getParameters();
        $cat = $params['Category'];
        // リクエストパラメータ送信時にエンコードしているため、もう１度デコードが必要
        $freeWd = Apm_Util::decodeValue($params['FreeWd']);

        if ($cat == 'area')
        {
            $code = $this->convCityCode($params['Code']);
            $this->makeTemplateParams($cat, $code, $freeWd);
            $this->displayBody2('result');
        }
        else if ($cat == 'area2')
        {
            $codes = array();
            foreach ($params['CityCd'] as $code)
            {
                $codes[] = $this->convCityCode($code);
            }
            $this->makeTemplateParams($cat, $codes, $freeWd);
            $this->displayBody2('result');
        }
        else if ($cat == 'line')
        {
            $codes = array();
            foreach ($params['RailCd'] as $code)
            {
                $codes[] = $this->convRailCode($code);
            }
            $this->makeTemplateParams($cat, $codes, $freeWd);
            $this->displayBody2('result_line');
        }
        else if ($cat == 'free')
        {
            $codes = array();
            $this->makeTemplateParams($cat, $codes, $freeWd);
            $this->displayBody2('result_free');
        }
    }

    public function postAction()
    {
        $this->indexAction();
    }

    private function convCityCode($code)
    {
        return substr($code, 0, 2) . '-' . $code;
    }

    private function convRailCode($code)
    {
        return substr($code, 0, 2) . '-' . substr($code, 2, 6);
    }

    private function getPrefNameByPrefCd($params)
    {
        $model1 = new Search_Model_Selareasearch();
        $data = $model1->getPrefNameByPrefCd($params);
        foreach ($data as $k => $v)
        {
            $data[$k]['PrefName'] = $this->getPrefName($v['M01_TODOUFUKEN_CD']);
        }
        return $data;
    }

    private function makeTemplateParams($cat, $code, $freeWd)
    {
        $params = array();

        $this->_data['FreeWd'] = $freeWd;

        if ($cat == 'area' || $cat == 'area2')
        {
            $params['SrhKbn'] = '01';
            // $params['FreeWd'] = $freeWd;
            $params = $this->addCodesToParams2($params, $code, 'PrefCd', 'CityCd');

            $model = new Shop_Model_ListForCity();
            $model_seo = new Shop_Model_SeoParamForCity();

            $this->_data['Mapping_pref'] = $this->getPrefNameByPrefCd($params);

            $model2 = new Search_Model_Selcyo();
            $this->_data['Mapping_city'] = $model2->getShikucyosonNameByShikucyosonCd($params);
        }
        elseif ($cat == 'line')
        {
            $params['SrhKbn'] = '02';
            // $params['FreeWd'] = $freeWd;
            $params = $this->addCodesToParams2($params, $code, 'PrefCd', 'RailCd');

            $model = new Shop_Model_ListForRail();
            $model_seo = new Shop_Model_SeoParamForRail();

            $this->_data['Mapping_pref'] = $this->getPrefNameByPrefCd($params);

            $model2 = new Search_Model_Selensen();
            $this->_data['Mapping_ensen'] = $model2->getRailNameByRailCd($params);
        }
        elseif ($cat == 'free')
        {
            $params['SrhKbn'] = '03';
            $params['FreeWd'] = $freeWd;
            $model = new Shop_Model_ListForCity();
            // $model_seo = new Shop_Model_SeoParamForCity();
        }

        $data = $model->getList($params);
        // $data = $model->convertList($data,'','',$params);

        // $seo_params = $model_seo->getList($params);

        $baseurl = $this->_config['apm']['baseurl'] . 'shop/shopdtl';
        $tenpoimageurl = $this->_config['apm']['apl']['imagesvurl'] . 'fs/tenpoimg/';

        $data = $model->convertList($data, $tenpoimageurl, $url, $params);

        $this->_data['Mapping'] = $data;
        $this->_data['Mapping_tenpoimageurl'] = $tenpoimageurl;

        $meta_data = Apm_Bukkenurl::setCommonMeta($params['SrhKbn'], $params, $this->_data['research_data'], true);

        if ($cat == 'area' || $cat == 'area2')
        {
            $this->_data['Mapping_area_name'] = $meta_data['AREA_FULL'];
            $this->_data['Mapping_area_name_other'] = $meta_data['AREA_FULL_OTHER'];
        }
        else if ($cat == 'line')
        {
            $this->_data['Mapping_area_name'] = $meta_data['STATION_CAT2'];
        }
        $this->_data['Mapping_cat'] = $cat;
        $this->_data['RandomPrefs'] = $this->getRandomPrefs(5);
        $this->_data['seo_search_data']['KODAWARI'] = Apm_Seo::getSeoImpKodawari(0, 6);
    }

    /**
     * SEO対策用のmetaタグに配置する文章を設定
     */
    protected function setSeoMeta()
    {
        $params = $this->getParameters();

        // フリーワード検索以外
        if ($params['Category'] != 'free')
        {
            $word = '';

            if (array_key_exists('Mapping_city', $this->_data))
            {
                $word = $this->_data['Mapping_city'][0]['M01_SHIKUCHOUSON'];
            }
            else if (array_key_exists('Mapping_ensen', $this->_data))
            {
                $word = $this->_data['Mapping_ensen'][0]['M03_ENSEN_NAME'];
            }

            $this->_meta_title = $word . 'のアパマンショップを探す 【アパマンショップ】';
            $this->_meta_description = $word . 'のアパマンショップでお部屋を探すなら、不動産情報が満載のアパマンショップネットワーク。賃貸マンション・アパートなど、';
            $this->_meta_keywords = $word . ' アパマンショップ,';

            if ($params['Category'] == 'area')
            {
                $this->_alternate_url = 'sp/' . $this->_data['Mapping_pref'][0]['PrefName'] . '/' . $params['Code'] . '/';
            }
            else if ($params['Category'] == 'area2')
            {
                $this->_alternate_url = 'sp/' . $this->_data['Mapping_pref'][0]['PrefName'] . '/' . $params['CityCd'][0] . '/';
            }
        }
    }

}
?>